
/* GCSx
** SCRIPT.H
**
** Script support
** Doesn't include any editor-only functionality
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#ifndef __GCSx_SCRIPT_H_
#define __GCSx_SCRIPT_H_

class Script : virtual public LoadOnly {
protected:
    // Is the script cached to our file and not in memory?
    int cached;
    FileRead* cacheFile;
    int lockCount;

    // Name of script, where it's located
    std::string name;
    std::string nameL;
    class World* world; // NULL if not yet added to a world
    
    // Numeric ID- computer-generated- unique to world- nonzero
    int id;
    
    // Notes are just scripts that aren't compiled
public:
    enum ScriptType {
        SCRIPT_CODE = 0,
        SCRIPT_NOTE = 1,
        SCRIPT_LIBRARY = 2,
    };
protected:
    ScriptType scriptType;
    
    // Default sprite representation (can be changed individually)
    // Only one (animgroup or tileset) can be set, but both can be NULL
    // Script never locks these
    class AnimGroup* defaultAnimgroup;
    class TileSet* defaultTileset;
    int defaultId; // (which anim or tile to use)
    
    // Status of info on disk and in memory may differ
public:
    enum ScriptStatus {
        CODE_UNCOMPILED = 0,
        CODE_PARSED,
        CODE_COMPILED,
        CODE_LINKED // Not valid for disk status
    };
protected:
    ScriptStatus diskStatus;
    int diskSource; // Is disk source present?
    ScriptStatus memStatus;
    int compileWarnings;
    int compileErrors;

    // Not cached- part of header, as needed to link other scripts too
    FunctionMap* functions; // Matches memStatus
    
    // @TODO: security checks/tainting? (do on a global level for any
    // unique filename+timestamp)
    
    // Data (cacheable) uncompiled and compiled
    std::list<std::string> source; // May not be present during gameplay
    Uint32* bytecode; // Matches memStatus
    std::list<struct LinkEntry>* links; // Present whenever bytecode is
    
    void doLink();
    
public:
    // Id must be unique; use ID of 0 if we're going to load anyways
    // World and ID may be NULL/0 if in process of creating
    Script(class World* myWorld, ScriptType type = SCRIPT_CODE, int myId = 0); // Starts with default settings
    virtual ~Script();

    // Must LOCK first
    // These only compile as needed
    // Errors may result
    virtual void parseFuncs();
    virtual void compile(); // Includes parse if not done
    // ASSERTS compilation and no errors; safe to call even if linked
    void collectGlobalLinks(World::GlobalMap* globalLinks, int* globalLinksCount);
    // ASSERTS compilation and no errors; safe to call even if linked
    // Normally called either pre-game, or by the entity using the script
    virtual void link();

    int numWarnings() const { return compileWarnings; }
    int numErrors() const { return compileErrors; }
    ScriptStatus status() const { return std::max(diskStatus, memStatus); }
    
    // Accessors
    const class World* getWorld() const { return world; }
    class World* getWorld() { return world; }
    int getId() const { return id; }
    int getBlockType() const { return WorldFileLoad::BLOCKTYPE_SCRIPT; }
    const std::string& getName() const { return name; }
    const std::string& getNameL() const { return nameL; }
    ScriptType getType() const { return scriptType; }
    class AnimGroup* getDefaultAnimgroup() { return defaultAnimgroup; }
    class TileSet* getDefaultTileset() { return defaultTileset; }
    int getDefaultId() const { return defaultId; }
    
    // Must be locked, compiled, and linked!
    Uint32* getCode();
    
    // File access
    void loadHeader(FileRead* file) throw_File;
    void loadContent(FileRead* file);
    int isContentCached() const;
    void cacheLoad() throw_File;
    int markLock() throw_File;
    int markUnlock();
    int isLocked() const;
    
    // Any content-accessor functions- remember to call markLock first!
};

#endif

